package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.domain.AlipayInfo;
import com.example.mall.dto.AlipayInfoDto;
import com.example.mall.mapper.AlipayInfoMapper;
import com.example.mall.query.AlipayInfoQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.service.IAlipayInfoService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 支付宝支付信息表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-06-03
 */
@Service
public class AlipayInfoServiceImpl extends ServiceImpl<AlipayInfoMapper, AlipayInfo> implements IAlipayInfoService {

    @Autowired
    private AlipayInfoMapper alipayInfoMapper;

    @Override
    public PageDTO<AlipayInfoDto> queryPage(AlipayInfoQuery alipayInfoQuery) {
        if (alipayInfoQuery == null) {
            alipayInfoQuery = new AlipayInfoQuery();
        }
        Page<AlipayInfo> page = alipayInfoQuery.toMpPageByDefaultCreateTimeAsc();
        Page<AlipayInfo> cartPage = lambdaQuery()
                .like(alipayInfoQuery.getId()!= null, AlipayInfo::getId, alipayInfoQuery.getId())
                .like(alipayInfoQuery.getAlipayPublicKey()!= null, AlipayInfo::getAlipayPublicKey, alipayInfoQuery.getAlipayPublicKey())
                .like(alipayInfoQuery.getAppId()!= null, AlipayInfo::getAppId, alipayInfoQuery.getAppId())
                .like(alipayInfoQuery.getMerchantPrivateKey()!= null, AlipayInfo::getMerchantPrivateKey, alipayInfoQuery.getMerchantPrivateKey())
                .like(alipayInfoQuery.getProtocol()!= null, AlipayInfo::getProtocol, alipayInfoQuery.getProtocol())
                .like(alipayInfoQuery.getGatewayHost()!= null, AlipayInfo::getGatewayHost, alipayInfoQuery.getGatewayHost())
                .like(alipayInfoQuery.getSignType()!= null, AlipayInfo::getSignType, alipayInfoQuery.getSignType())
                .like(alipayInfoQuery.getNotifyUrl()!= null, AlipayInfo::getNotifyUrl, alipayInfoQuery.getNotifyUrl())
                .like(alipayInfoQuery.getReturnUrl()!= null, AlipayInfo::getReturnUrl, alipayInfoQuery.getReturnUrl())
                .orderByDesc(AlipayInfo::getUpdateTime)
                .page(page);
        return PageDTO.of(cartPage, AlipayInfoDto.class);
    }

    @Override
    public AlipayInfo queryAlipayInfoById(Long id) {
        AssertUtil.isNotNull(id, "id不能为空");
        return alipayInfoMapper.selectById(id);
    }

    @Override
    public List<AlipayInfo> queryAlipayInfoList() {
        return this.list();
    }

    @Override
    public void insert(AlipayInfoDto alipayInfoDto) {
        AlipayInfo alipayInfo = new AlipayInfo();
        CopyUtils.copyProperties(alipayInfoDto, alipayInfo);
        alipayInfo.setId(CodeGenerateUtils.generateUniqueId());
        alipayInfo.setCrateTime(new Date());
        alipayInfo.setUpdateTime(new Date());
        alipayInfoMapper.insert(alipayInfo);
    }

    @Override
    public void update(AlipayInfoDto alipayInfoDto) {
        AssertUtil.isNotNull(alipayInfoDto.getId(), "id不能为空");
        AlipayInfo alipayInfo = alipayInfoMapper.selectById(alipayInfoDto.getId());
        AssertUtil.isNotNull(alipayInfo, "支付宝支付信息不存在");
        CopyUtils.copyProperties(alipayInfoDto, alipayInfo);
        alipayInfo.setUpdateTime(new Date());
        alipayInfoMapper.updateById(alipayInfo);
    }

    @Override
    public void delete(Long id) {
        AssertUtil.isNotNull(id, "id不能为空");
        AlipayInfo alipayInfo = alipayInfoMapper.selectById(id);
        AssertUtil.isNotNull(alipayInfo, "支付宝支付信息不存在");
        alipayInfoMapper.deleteById(id);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        AssertUtil.isNotNull(ids, "id列表不能为空");
        List<AlipayInfo> alipayInfos = alipayInfoMapper.selectBatchIds(ids);
        AssertUtil.isNotNull(alipayInfos, "支付宝支付信息不存在");
        alipayInfoMapper.deleteBatchIds(ids);
    }
}
